package day20210626;

import treenode.SimpleTreeNode;

/**
 * 解决方案
 * 112. 路径总和
 * 给你二叉树的根节点 root 和一个表示目标和的整数 targetSum ，
 * 判断该树中是否存在 根节点到叶子节点 的路径，
 * 这条路径上所有节点值相加等于目标和 targetSum 。
 *
 * 叶子节点 是指没有子节点的节点。
 *
 * @author lizy
 * @date 2021/06/26
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        SimpleTreeNode simpleTreeNode = new SimpleTreeNode();
        simpleTreeNode.val = 1;
        simpleTreeNode.right = new SimpleTreeNode(2);
        simpleTreeNode.right.left = new SimpleTreeNode(3);
        System.out.println(solution.hasPathSum(simpleTreeNode,6));
    }


    private boolean hasPathSum(SimpleTreeNode root, int targetSum) {
        if (root == null){
            return false;
        }
        if (root.left == null && root.right == null){
            return targetSum - root.val == 0;
        }
        return hasPathSum(root.right,targetSum - root.val) || hasPathSum(root.left,targetSum - root.val);
    }
}
